<template>
  <div id="itemBank">
    <router-view v-bind={question,daan,handleNext,handleBack,flag}></router-view>
  </div>
</template>

<script>
import { MessageBox } from 'mint-ui';
export default {
  name: "App",
  data(){
    return{
      questionBank:[],
      answer:[],
      personal:[],
      teachertextitembankid:'',
      openid:'',
      name:'',
      age:'',
      phonenum:'',
      data:[]
    }
  },
  computed:{
    flag:function(){
      return this.$route.params.id-1;
    },
    question:function(){
      return this.questionBank[this.flag]
    },
    daan:function(){
      return this.answer.find(item=>item.id==this.flag+1)
    }
  },
  methods:{  
    // 下一题
    handleNext(data){
      const d = this.answer.findIndex(item=>item.id==this.flag+1)
      if(d !== -1) {
        this.answer.splice(d, 1, data)
      }else {
        this.answer.push(data)
      }
      console.log(this.questionBank)
      const num = Number(this.$route.params.id)+1
      if(num===this.questionBank.length+1){
        this.handleEnter()
      }else{
       this.$router.push('/itemBank/'+num)
      }
    },
    //上一题
    handleBack(data){
      const d = this.answer.findIndex(item=>item.id==this.flag+1)
      if(d !== -1) {
        this.answer.splice(d, 1, data)
      }else {
        this.answer.push(data)
      }
      if(this.flag==0){
        console
      }else{
        this.$router.push('/itemBank/'+this.flag)
      }
    },
    // 提交
    handleEnter(){
      MessageBox({
        title: '提示',
        message: '确定提交答案？',
        showCancelButton: true
      }).then(action => {
        if (action === 'confirm') {
           this.data.data=this.answer
            this.data.age=this.personal[0].age

            //提交
            this.axios({
              method: "post",
              url: "/v4/sys/addtestanswer",
              data:{
                // testanswer:testanswer
                teachertextitembankid:this.teachertextitembankid,
                openid:this.openid,
                // info:this.personal[0].age,
                name:this.personal[0].name,
                phonenum:this.personal[0].phone,
                data:this.data
              },
              xsrfCookieName: 'csrfToken',
              xsrfHeaderName: 'x-csrf-token',
            }).then((data)=>{
              console.log(data)
            }).catch((err)=>{
              console.log(err)
            })

            this.$router.push('/submit')
        }
        if (action === 'cancel') {
          console.log(2)
        }
      }).catch(err => {
        console.log(err)
      })
    }
    /* handleEnter(){
      this.data.data=this.answer
      this.data.age=this.personal[0].age

      //提交
       this.axios({
        method: "post",
        url: "/v4/sys/addtestanswer",
        data:{
          // testanswer:testanswer
          teachertextitembankid:this.teachertextitembankid,
          openid:this.openid,
          // info:this.personal[0].age,
          name:this.personal[0].name,
          phonenum:this.personal[0].phone,
          data:this.data
        },
        xsrfCookieName: 'csrfToken',
        xsrfHeaderName: 'x-csrf-token',
      }).then((data)=>{
        console.log(data)
      }).catch((err)=>{
        console.log(err)
      })

      this.$router.push('/submit')
      
    } */
  },
  created(){
    this.questionBank=JSON.parse(localStorage.getItem('questionBank')),
    this.personal=JSON.parse(localStorage.getItem('personal')),
    this.teachertextitembankid=JSON.parse(localStorage.getItem('teachertextitembankid')),
    this.openid=JSON.parse(localStorage.getItem('openid'))
    this.data=JSON.parse(localStorage.getItem('data'))
  },
};                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
</script>
<style scoped>
.active{
  background-color: #4086f2 !important;
  color:#fff
}
.disappear{
  background-color: #edf4fd !important;
}
header {
  height: 44px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 16px;
  border-bottom: 1px solid #eaeaea;
}
.headType {
  width: 65px;
  height: 25px;
  border: 1px solid #bfd9fb;
  background-color: #edf4fd;
  text-align: center;
  line-height: 25px;
  color: #69a5f8;
  font-size: 14px;
  border-radius: 4px;
}
.content {
  height: 360px;
  padding:10px;
  overflow-y: auto;
}
.section {
  padding-left: 16px;
  height: 30px;
  border-bottom: 1px solid #eaeaea;
}
.key {
  min-height: 70px;
  border-bottom: 1px solid #eaeaea;
  display: flex;
  align-items: center;
  justify-content: center;
}
.key span {
  height: 25px;
  width: 25px;
  background-color: #f7f7f7;
  font-size: 12px;
  font-weight: bold;
  display: block;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 3px;
}
footer {
  display: flex;
  height: 70px;
  align-items: center;
  justify-content: space-around;
  padding: 0 20px;
}
footer span {
  width: 125px;
  height: 45px;
  display: block;
  color: #fff;
  background-color: #4086f2;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 30px;
}
.text{
  width:200px;
  height:30px;
  border-radius:10px;
  outline:none;
  border:1px solid #eaeaea;
  padding-left:3px;
  margin-left:6px;
}
</style>